package com.example.admin.kline;

import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CandleStickActivity extends AppCompatActivity {
    private CandleStickChart stickChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_candle_stick);
        initView();
    }

    private void initView() {
        stickChart=findViewById(R.id.stickChart);
        stickChart.setScaleEnabled(true);//启用图表缩放事件
        stickChart.setDrawBorders(true);//是否绘制边线
        stickChart.setBorderWidth(1);//边线宽度，单位dp
        stickChart.setDragEnabled(true);//启用图表拖拽事件
        stickChart.setScaleYEnabled(false);//启用Y轴上的缩放

        Description description=new Description();
        description.setEnabled(false);
        stickChart.setDescription(description);//右下角对图表的描述信息
        stickChart.setMinOffset(0f);
        stickChart.setExtraOffsets(0f, 0f, 0f, 3f);
//        //X轴设置
        XAxis xAxis=stickChart.getXAxis();
        xAxis.setDrawGridLines(false);//是否显示X坐标轴上的刻度竖线，默认是true
        xAxis.setDrawAxisLine(true); //是否绘制坐标轴的线，即含有坐标的那条线，默认是true
        xAxis.setTextColor(R.color.colorText);
        xAxis.setAxisLineColor(R.color.colorLine);
        xAxis.setGridColor(R.color.colorLine);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.enableGridDashedLine(10f, 10f, 0f);//虚线表示X轴上的刻度竖线(float lineLength, float spaceLength, float phase)三个参数，1.线长，2.虚线间距，3.虚线开始坐标
        xAxis.setAvoidFirstLastClipping(true);//设置首尾的值是否自动调整，避免被遮挡
//
        //Y轴设置
        YAxis  axisLeftKline = stickChart.getAxisLeft();
        axisLeftKline.setDrawGridLines(true);
        axisLeftKline.setDrawAxisLine(false);
        axisLeftKline.setDrawZeroLine(false);
        axisLeftKline.setDrawLabels(true);
        axisLeftKline.enableGridDashedLine(10f, 10f, 0f);
        axisLeftKline.setTextColor(R.color.colorText);
//        axisLeftKline.setGridColor(getResources().getColor(R.color.minute_grayLine));
        axisLeftKline.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        axisLeftKline.setLabelCount(100, false); //第一个参数是Y轴坐标的个数，第二个参数是 是否不均匀分布，true是不均匀分布
//        axisLeftKline.setSpaceTop(10);//距离顶部留白

        YAxis axisRightKline = stickChart.getAxisRight();
        axisRightKline.setDrawLabels(false);
        axisRightKline.setDrawGridLines(false);
        axisRightKline.setDrawAxisLine(false);
        axisRightKline.setLabelCount(4, false); //第一个参数是Y轴坐标的个数，第二个参数是 是否不均匀分布，true是不均匀分布

        //数据
        List<CandleEntry> data=getCandleEntries();
        CandleDataSet set=new CandleDataSet(data,"Data Set");
        set.setAxisDependency(YAxis.AxisDependency.LEFT);
        set.setShadowColor(Color.DKGRAY);//影线颜色
        set.setShadowColorSameAsCandle(true);//影线颜色与实体一致
        set.setShadowWidth(0.7f);//影线
        set.setDecreasingColor(Color.RED);//涨
        set.setDecreasingPaintStyle(Paint.Style.FILL);//红涨，实体
        set.setIncreasingColor(Color.GREEN);//跌
        set.setIncreasingPaintStyle(Paint.Style.STROKE);//绿跌，空心
        set.setNeutralColor(Color.RED);//当天价格不涨不跌（一字线）颜色
        set.setHighlightLineWidth(1f);//选中蜡烛时的线宽
        set.setDrawValues(false);//在图表中的元素上面是否显示数值
        set.setLabel("label");//图表名称，可以通过mChart.getLegend().setEnable(true)显示在标注上
    }

    private List<CandleEntry> getCandleEntries() {
        List<CandleEntry> entries=new ArrayList<>();
        Random random=new Random();
        for (int i=0;i<80;i++){
            int open= random.nextInt(50);
            int close= random.nextInt(50);
            int max= random.nextInt(50);
            int min= random.nextInt(50);
            CandleEntry candleEntry=new CandleEntry(i,open,close,max,min);
            entries.add(candleEntry);
        }
        return  entries;
    }
}
